package com.prj.ufdm.webflux.config;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.config.WebFluxConfigurer;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.prj.ufdm.webflux.dto.RetData;
import com.prj.ufdm.webflux.enums.WebFluxRetCodeEnum;

@Configuration
@RestController
public class WebFluxConfig implements WebFluxConfigurer {
	
//	@Bean
//	public FactoryBean<UfdmHttp> registerFactoryBean() {
//		return new TestFactoryBeanHandler<UfdmHttp>(UfdmHttp.class);
//	}
	
	/**
	  * 使用 FastJson 
	 * @return
	 */
	@Bean
	public HttpMessageConverters fastJsonHttpMessageConverters() {
        // 定义一个转换消息的对象
		FastJsonHttpMessageConverter fastConverter4 = new FastJsonHttpMessageConverter();
		// 配置信息
		FastJsonConfig fastJsonConfig = new FastJsonConfig();
		fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
		fastJsonConfig.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect);
		
        fastJsonConfig.setCharset(Charset.forName("UTF-8"));
        // 媒体类型
        List<MediaType> fastMediaTypes = new ArrayList<MediaType>();
        fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
		// 在转换器中添加配置信息
		fastConverter4.setFastJsonConfig(fastJsonConfig);
		fastConverter4.setSupportedMediaTypes(fastMediaTypes);
		return new HttpMessageConverters(fastConverter4);
	}   
	

    @RequestMapping(value = "/authfallback")
    public RetData<String> authFallBackController() {
    	RetData<String> retData = new RetData<String>();
		retData.getRetHead().setCode(WebFluxRetCodeEnum.RET_CODE_0899901.getCode());
		retData.getRetHead().setMessage(WebFluxRetCodeEnum.RET_CODE_0899901.getMessage());
		return retData;
    }
    
    @RequestMapping(value = "/testfallback")
    public RetData<String> testFallBackController() {
    	RetData<String> retData = new RetData<String>();
		retData.getRetHead().setCode(WebFluxRetCodeEnum.RET_CODE_0899902.getCode());
		retData.getRetHead().setMessage(WebFluxRetCodeEnum.RET_CODE_0899902.getMessage());
		return retData;
    }
}
